package org.sxf.app.sign.activity;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.util.LogUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import org.sxf.app.sign.R;
import org.sxf.app.sign.fragment.AppFragment;
import org.sxf.app.sign.fragment.BaseFragment;
import org.sxf.app.sign.fragment.MyFragment;
import org.sxf.app.sign.provider.LocProviderSingleton;

public class MainTabActivity extends BaseActivity implements View.OnTouchListener {


    @ViewInject(R.id.app_tab_bt)
    private RelativeLayout mTabBtApp;
    @ViewInject(R.id.my_tab_bt)
    private RelativeLayout mTabBtMy;

    private String indexNow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_tab);

        ViewUtils.inject(this);

        initData();
        initUI();

        LocProviderSingleton.getInstance().initProvider(getApplicationContext());
    }

    private void initUI(){
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        AppFragment indexFragment = new AppFragment();
        indexFragment.setRetainInstance(true);
        fragmentTransaction.add(R.id.fragment_container, indexFragment, "0").commitAllowingStateLoss();

        mTabBtApp.setSelected(true);
        mTabBtApp.setOnTouchListener(this);
        mTabBtMy.setOnTouchListener(this);
    }

    private void initData(){
        indexNow = "0";
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (view.getTag() instanceof String){
            String tag = (String)view.getTag();

            try {
                if (indexNow.equals(tag)){
                    return true;
                }

                refreshFragment(tag);
            }catch (Exception e){

            }
        }
        return true;
    }


    private void refreshFragment(String tag){




        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        BaseFragment fragment;
        BaseFragment fragmentFrom;
        switch (tag){
            case "0":

                mTabBtApp.setSelected(true);
                mTabBtMy.setSelected(false);

                fragment = (BaseFragment)fragmentManager.findFragmentByTag("0");
                if (fragment == null){
                    fragment = new AppFragment();
                    fragment.setRetainInstance(true);
                }
                fragmentFrom = (BaseFragment)fragmentManager.findFragmentByTag(indexNow);
                switchFragment(fragmentFrom, fragment, tag);

                break;
            case "1":

                mTabBtApp.setSelected(false);
                mTabBtMy.setSelected(true);

                fragment = (BaseFragment)fragmentManager.findFragmentByTag("1");
                if (fragment == null){
                    fragment = new MyFragment();
                    fragment.setRetainInstance(true);
                }
                fragmentFrom = (BaseFragment)fragmentManager.findFragmentByTag(indexNow);
                switchFragment(fragmentFrom, fragment, tag);

                break;

        }

        fragmentTransaction.commitAllowingStateLoss();

    }

    public void switchFragment(Fragment from, Fragment to, String toTag){
        if (from == null || to == null)
            return;
        FragmentTransaction transaction = getFragmentManager()
                .beginTransaction();
        if (!to.isAdded()) {
            // 隐藏当前的fragment，add下一个到Activity中
            LogUtils.d("add fragment," + toTag);
            transaction.hide(from).add(R.id.fragment_container, to, toTag).commitAllowingStateLoss();
        } else {
            // 隐藏当前的fragment，显示下一个
            LogUtils.d("replace fragment," + toTag);
            transaction.hide(from).show(to).commitAllowingStateLoss();
        }

        indexNow = toTag;
    }


}
